<?php

namespace app\controller\admin\store;


use crmeb\basic\BaseController;
use app\common\repositories\store\product\ProductReplyTemplateRepository;
use app\validate\admin\StoreProductReplyTemplateValidate;
use think\App;

class StoreProductReplyTemplate extends BaseController
{
    /**
     * @var ProductReplyTemplateRepository
     */
    protected $repository;

    public function __construct(App $app, ProductReplyTemplateRepository $repository)
    {
        parent::__construct($app);
        $this->repository = $repository;
    }

    public function lst()
    {
        [$page, $limit] = $this->getPage();
        $where = $this->request->params(['keyword', 'lvl', 'cate_id']);
        return app('json')->success($this->repository->getList($where, $page, $limit));
    }


    public function detail($id)
    {
        $data = $this->repository->detail($id);
        return app('json')->success($data);
    }

    public function createForm()
    {
        return app('json')->success(formToData($this->repository->form(null)));
    }

    public function create()
    {
        $data = $this->checkParams();
        $this->repository->save($data);
        return app('json')->success('添加成功');
    }


    public function updateForm($id)
    {

        return app('json')->success(formToData($this->repository->form($id)));
    }

    public function update($id)
    {
        $data = $this->checkParams();
        $this->repository->edit($id, $data);
        return app('json')->success('编辑成功');
    }

    public function delete($id)
    {
        if (!$this->repository->exists($id))
            return app('json')->fail('数据不存在');
        $this->repository->delete($id);
        return app('json')->success('删除成功');
    }

    public function checkParams()
    {
        $validate = (new StoreProductReplyTemplateValidate());
        $data = $this->request->params([ 'cate_id', 'comment', 'lvl', 'title', 'sort']);
        $validate->check($data);
        return $data;
    }




}
